import { USER_ROLES, USER_ROLE_NAMES } from '@/types'
import type { User } from '@/types'

/**
 * 判断用户是否为管理员
 */
export const isAdmin = (user: User | null): boolean => {
  return user?.userRole === USER_ROLES.ADMIN || user?.role === 'admin'
}

/**
 * 判断用户是否为普通用户
 */
export const isNormalUser = (user: User | null): boolean => {
  return user?.userRole === USER_ROLES.USER || user?.role === 'user'
}

/**
 * 获取用户角色名称
 */
export const getUserRoleName = (userRole: number): string => {
  return USER_ROLE_NAMES[userRole as keyof typeof USER_ROLE_NAMES] || '未知角色'
}

/**
 * 根据用户角色获取默认跳转路径
 */
export const getDefaultRedirectPath = (userRole: number): string => {
  switch (userRole) {
    case USER_ROLES.ADMIN:
      return '/admin'
    case USER_ROLES.USER:
      return '/user/profile'
    default:
      return '/'
  }
}

/**
 * 检查用户是否有权限访问指定路径
 */
export const hasPermission = (user: User | null, requiresAdmin: boolean = false): boolean => {
  if (!user) return false
  
  if (requiresAdmin) {
    return isAdmin(user)
  }
  
  return true
} 